package com.wangsanfei.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.wangsanfei.util.StringUtils;
import com.wangsanfei.util.UUIDUtils;

public class User extends Model<User>{

	private static final long serialVersionUID = -2929579257546666751L;
	public static final User dao = new User();

	public User getUserByUsernameAndPassword(String username,String password){
		if(StringUtils.isEmpty(username)){
			return null;
		}
		if(StringUtils.isEmpty(password)){
			return null;
		}
		
		return this.findFirst("select * from t_user t where t.removed='0' and t.username=? and password=?",username,password);
	}
	
	public User getUserByUsername(String username){
		return this.findFirst("select * from t_user t where t.removed='0'  and t.username=?",username);
	}
	
	public Page<Record> pagequery(Integer pageNumber,Integer pageSize,User user,Record record){
		StringBuffer sqlExceptSelect=new StringBuffer("from t_user t where t.removed='0' and t.userType='2'  ");//只查角色为普通用户的用户
		List<Object> list=new ArrayList<Object>();
		
		if(!StringUtils.isEmpty(record.getStr("searchContent"))){
			sqlExceptSelect.append(" and (t.merLinId like ? or t.englishName like ?)");
			list.add("%"+record.getStr("searchContent")+"%");
			list.add("%"+record.getStr("searchContent")+"%");
		}
		
		sqlExceptSelect.append(" order by t.createTime desc,t.englishName,t.holidexCode,t.merLinId");
		
		if(pageNumber==null){
			pageNumber=1;
		}
		if(pageSize==null){
			pageSize=10;
		}
		return Db.paginate(pageNumber,pageSize,"select t.* ",sqlExceptSelect.toString(),list.toArray());
	}
	
	public User add(User vo){
		User user=new User();
		user.set("id", UUIDUtils.getUUID());
		user.set("username", vo.getStr("username"));
		user.set("password", vo.getStr("password"));
		user.set("createTime", new Date());
		user.set("updateTime", new Date());
		user.set("updator", vo.getStr("updator"));
		user.set("creator", vo.getStr("creator"));
		user.set("removed", "0");
		user.set("brandId",vo.getStr("brandId"));
		user.set("hotelId",vo.getStr("hotelId"));
		user.set("deptId",vo.getStr("deptId"));
		user.set("userType",vo.getStr("userType"));
		
		user.set("email",vo.getStr("email"));
		user.set("nickname",vo.getStr("englishName"));
		user.set("englishName",vo.getStr("englishName"));
		user.set("merLinId",vo.getStr("merLinId"));
		user.set("holidexCode",vo.getStr("holidexCode"));
		
		user.save();
		return user;
	}
	
	public User edit(User vo){
		User user=User.dao.findById(vo.getStr("id"));
		user.set("updateTime", new Date());
		user.set("updator", vo.getStr("updator"));
		user.set("holidexCode", vo.getStr("holidexCode"));
		user.set("englishName",vo.getStr("englishName"));
		user.set("email",vo.getStr("email"));
		
		user.update();
		return user;
	}
	
	
	public User editInfo(User vo){
		User user=User.dao.findById(vo.getStr("id"));
		user.set("updateTime", new Date());
		user.set("updator", vo.getStr("updator"));
	
		user.set("weixin",vo.getStr("weixin"));
		user.set("mail",vo.getStr("mail"));
		user.set("position",vo.getStr("position"));
		
		if(vo.getStr("photoId")!=null&&!vo.getStr("photoId").equals("")){
			user.set("photoId", vo.getStr("photoId"));
		}
		
		user.update();
		return user;
	}
	
	
	public User remove(String id){
		User user=User.dao.findById(id);
		user.set("removed", "1");
		user.update();
		return user;
	}
	
	public Record getUser(String username){
		if(!StringUtils.isEmpty(username)){
			username=username.toUpperCase();
		}
		String sql="select * from t_user t where upper(t.username)=? and t.removed='0'";
		return Db.findFirst(sql,username);
	}
	
	public Record getUserById(String userid){
		
		String sql="select * from t_user t where id=? and t.removed='0'";
		return Db.findFirst(sql,userid);
	}
	
	public List<Record> getUserByHotel(String brandId,String hotelId,String deptId){
		String sql="select * from t_user t where t.removed='0' and t.brandId=? and hotelId=? and deptId=?";
		return Db.find(sql,brandId,hotelId,deptId);
	}
	
	public Record getMerLinId(String merLinId){
		String sql="select * from t_merlinid_name t where UPPER(t.merLinId)=?";
		return Db.findFirst(sql,merLinId);
	}
	
	public Record getUserByMerLinId(String merLinId){
		String sql="select * from t_user t where UPPER(t.merLinId)=?";
		return Db.findFirst(sql,merLinId);
	}
	
	public Record getUserByMerLinIdAndPassword(String merLinId,String password){
		String sql="select * from t_user t where UPPER(t.merLinId)=? and password=?";
		return Db.findFirst(sql,merLinId,password);
	}
	
}
